#include "keyboard.h"
#include <xkbcommon/xkbcommon.h>
#include "wlroots-full.hpp"

void keyboard_set_numlock(struct wlr_keyboard *keyboard)
{

}

static void
keyboard_key_notify(struct wl_listener *listener, void *data)
{
#if 0
    /* This event is raised when a key is pressed or released. */
    struct Keyboard *keyboard = wl_container_of(listener, keyboard, key);
    struct Seat *seat = keyboard->base.seat;
    struct wlr_keyboard_key_event *event = data;
    struct wlr_seat *wlr_seat = seat->seat;
    // idle_manager_notify_activity(seat->seat);

    /* any new press/release cancels current keybind repeat */
    keyboard_cancel_keybind_repeat(keyboard);

#if 0
    enum lab_key_handled handled =
        handle_compositor_keybindings(keyboard, event);

    if (handled == LAB_KEY_HANDLED_TRUE_AND_VT_CHANGED) {
        return;
    }

    if (handled) {
        if (event->state == WL_KEYBOARD_KEY_STATE_PRESSED) {
            start_keybind_repeat(seat->server, keyboard, event);
        }
    } else {
#endif
        wlr_seat_set_keyboard(wlr_seat, keyboard->wlr_keyboard);
        wlr_seat_keyboard_notify_key(wlr_seat, event->time_msec,
                                     event->keycode, event->state);
#if 0
    }
#endif
#endif
}

static void
keyboard_modifiers_notify(struct wl_listener *listener, void *data)
{
#if 0
    struct Keyboard *keyboard = wl_container_of(listener, keyboard, modifier);
    struct Seat *seat = keyboard->base.seat;
    struct Server *server = seat->server;
    struct wlr_keyboard *wlr_keyboard = keyboard->wlr_keyboard;

#if 0
    if (server->input_mode == LAB_INPUT_STATE_MOVE) {
        /* Any change to the modifier state re-enable region snap */
        seat->region_prevent_snap = false;
    }
#endif

#if 0
    if (server->osd_state.cycle_view || server->grabbed_view
        || seat->workspace_osd_shown_by_modifier) {
        if (!keyboard_any_modifiers_pressed(wlr_keyboard))  {
            if (server->osd_state.cycle_view) {
                if (key_state_nr_bound_keys()) {
                    should_cancel_cycling_on_next_key_release = true;
                } else {
                    end_cycling(server);
                }
            }
            if (seat->workspace_osd_shown_by_modifier) {
                workspaces_osd_hide(seat);
            }
            if (server->grabbed_view) {
                regions_hide_overlay(seat);
            }
        }
    }
#endif
    wlr_seat_keyboard_notify_modifiers(seat->seat, &wlr_keyboard->modifiers);
#endif
}


void keyboard_setup_handlers(struct Keyboard *keyboard)
{

}

void keyboard_cancel_keybind_repeat(struct Keyboard *keyboard)
{
    if (keyboard->keybind_repeat) {
        wl_event_source_remove(keyboard->keybind_repeat);
        keyboard->keybind_repeat = NULL;
    }
}

void Keyboard::setup_handlers()
{
    struct wlr_keyboard *wlr_kb = m_wlr_keyboard;

    keyboard->key.notify = keyboard_key_notify;
    wl_signal_add(&wlr_kb->events.key, &keyboard->key);
    keyboard->modifier.notify = keyboard_modifiers_notify;
    wl_signal_add(&wlr_kb->events.modifiers, &keyboard->modifier);
}

void Keyboard::set_numlock(wlr_keyboard *keyboard)
{
    xkb_mod_index_t num_idx =
        xkb_map_mod_get_index(keyboard->keymap, XKB_MOD_NAME_NUM);
    if (num_idx == XKB_MOD_INVALID) {
        wlr_log(WLR_INFO, "Failed to set Num Lock: not found in keymap");
        return;
    }

    xkb_mod_mask_t locked = keyboard->modifiers.locked;
#if 0
    if (rc.kb_numlock_enable) {
        locked |= (xkb_mod_mask_t)1 << num_idx;
    } else {
#endif
    locked &= ~((xkb_mod_mask_t)1 << num_idx);
#if 0
    }
#endif

    /*
     * This updates the xkb-state + kb->modifiers and also triggers the
     * keyboard->events.modifiers signal (the signal has no effect in
     * current labwc usage since the keyboard is not part of a
     * keyboard-group yet).
     */
    wlr_keyboard_notify_modifiers(keyboard, keyboard->modifiers.depressed,
                                  keyboard->modifiers.latched, locked, keyboard->modifiers.group);
}

void Keyboard::cancel_keybind_repeat()
{

}
